package com.vogtec.fota.mqtt;

import com.vogtec.fota.config.Config;
import com.vogtec.fota.dao.BikeDao;
import com.vogtec.fota.dao.UpgradeTaskDao;
import com.vogtec.fota.entity.mqtt.BikeFota;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by wei on 2017/7/15.
 */
@Component
@Slf4j
public class StartUpgrade {

    @Autowired
    private BikeEmqttClient bikeEmqttClient;

//    @Autowired
//    private MqttCallback getMqttCallback;

    @Autowired
    private BikeDao bikeDao;
    @Autowired
    private UpgradeTaskDao upgradeTaskDao;

    public void publish(String bikeNum,byte protocol,String url,String topic) throws MqttException{

        log.info("****** in StartUpgrade.publish ******");

        BikeFota bikeFota = new BikeFota(bikeNum,protocol,url);
        byte[] content = bikeFota.encode();

        bikeEmqttClient.publish(content,topic);

        upgradeTaskDao.updateStatus(1, Config.taskId);
        bikeDao.updateStatusByUpgrade_task(1, Config.taskId);

    }

//    public void arrive(String topic, MqttMessage message) throws Exception{
//        MqttCallback mqttCallback = getMqttCallback;
//        mqttCallback.messageArrived(topic, message);

//    }
}
